<template>
	<view class="mainpadding" style="padding-bottom: 150rpx;">
		
		<view class="ffffff mainpadding flexbetween radius " @click="rwlxshow=true">
			<view class="ershiba xiaohei fonweight">任务类型</view>
			<view class="flexright">
				<input class="ershiba xiaohui margin_right1 textright" v-model="task_category_name" placeholder="请选择任务类型" type="text"
					disabled="" />
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top" @click="sxshow=true"  v-if="task_category_id!=1">
			<view class="ershiba xiaohei fonweight">车辆属性</view>
			<view class="flexright">
				<input class="ershiba xiaohui margin_right1 textright" v-model="car_attr_text" placeholder="请选择车辆属性"
					type="text" disabled="" />
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top" @click="startFn()" v-if="task_category_id!=1">
			<view class="ershiba xiaohei fonweight">起始地址</view>
			<view class="flexright">
				<input class="ershiba xiaohui margin_right1 textright" v-model="start_address" placeholder="请选择"
					type="text" disabled="" />
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top" @click="endFn()">
			<view class="ershiba xiaohei fonweight">目的地址</view>
			<view class="flexright">
				<input class="ershiba xiaohui margin_right1 textright" v-model="end_address" placeholder="请选择"
					type="text" disabled="" />
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top" @click="changesx()" v-if="task_category_id!=1">
			<view class="ershiba xiaohei fonweight">任务属性</view>
			<view class="flexright">
				<input class="ershiba xiaohui margin_right1 textright" v-model="data_type" placeholder="请选择任务属性"
					type="text" disabled="" />
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top"  v-if="task_category_id!=1">
			<view class="ershiba xiaohei fonweight">航班/车次</view>
			<input class="ershiba xiaohui margin_right1 textright" v-model="carno" placeholder="请输入" type="text" />
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top">
			<view class="ershiba xiaohei fonweight">客户姓名</view>
			<input class="ershiba xiaohui margin_right1 textright" v-model="name" placeholder="请输入" type="text" />
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top">
			<view class="ershiba xiaohei fonweight">客户电话</view>
			<input class="ershiba xiaohui margin_right1 textright" v-model="mobile" placeholder="请输入" type="number" />
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top">
			<view class="ershiba xiaohei fonweight">奖励积分</view>
			<input class="ershiba xiaohui margin_right1 textright" v-model="score" placeholder="请输入" type="number" />
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top">
			<view class="ershiba xiaohei fonweight">订单收入</view>
			<input class="ershiba xiaohui margin_right1 textright" v-model="price" placeholder="请输入" type="number" />
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top" @click="messageTime.timeShow=true">
			<view class="ershiba xiaohei fonweight">任务时间</view>
			<view class="flexright">
				<input class="ershiba xiaohui margin_right1 textright" v-model="tasktime" placeholder="请选择任务时间"
					type="text" disabled="" />
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding flexbetween radius margin_top" @click="jsdwshow=true">
			<view class="ershiba xiaohei fonweight">结算单位</view>
			<view class="flexright">
				<input class="ershiba xiaohui margin_right1 textright" v-model="settlement_text" placeholder="请选择结算单位"
					type="text" disabled="" />
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</view>
		</view>
		<view class="gudingdb mainpadding">
			<view class="bigbtn" @click="submit()">确认</view>
		</view>
		<template v-if="show1">
			<u-picker :show="jsdwshow" title="选择结算单位" keyName="name" @cancel="jsdwshow = false" :columns="jsdwcolumns"
				@confirm="jsdwconfirm"></u-picker>
			
		</template>
		<template v-if="show2">
			<u-picker :show="sxshow" title="选择车辆属性" keyName="name" @cancel="sxshow = false" :columns="sxcolumns"
				@confirm="sxconfirm"></u-picker>
		</template>
		<template v-if="show3">
			<u-picker :show="rwlxshow" title="选择任务类型" keyName="name" @cancel="rwlxshow = false" :columns="rwlxcolumns"
				@confirm="rwlxconfirm"></u-picker>
		</template>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				type: "",
				start_address: "",
				start_longitude: "",
				start_latitude: "",
				end_address: "",
				end_longitude: "",
				end_latitude: "",
				mobile: "",
				tasktime: "",
				score: "",
				data_type: "",
				name: "",
				carno: "",
				car_attr_id: "",
				car_attr_text: "",
				price: "",
				settlement_id: "",
				settlement_text: "",
				sxshow: false,
				sxcolumns: [],
				jsdwshow: false,
				jsdwcolumns: [],
				messageTime: {
					timeShow: false,
					title: "选择任务时间",
					mode: "datetime", //date日期，datetime年月日时分秒，time时间，year-month年月
					minDate: 0 //最小时间
				},
				listid:"",
				show1:false,
				show2:false,
				rwlxshow: false,
				rwlxcolumns: [],
				show3:false,
				task_category_id:"",
				task_category_name:"",
			}
		},
		onLoad(options) {
			if(options.id){
				this.listid = options.id
				uni.setNavigationBarTitle({
					title:"编辑任务"
				})
				this.getDetail()
			}
			this.getjsdw()
			this.getsx()
			this.getrwlx()
		},
		methods: {
			getDetail(){
				httpRequest.request('/api/taskcl/taskDetail', 'GET', {
					id:this.listid,
					lat:"",
					lng:"",
				}, false, false, true).then(res => {
					let data = res.data
					// this.type=data.type==1?'接送机任务':"其他任务"
					this.start_address=data.start_address
					this.start_longitude=data.start_longitude || 0
					this.start_latitude=data.start_latitude || 0
					this.end_address=data.end_address
					this.end_longitude=data.end_longitude
					this.end_latitude=data.end_latitude
					this.mobile=data.mobile
					this.tasktime=data.tasktime_text
					this.score=data.score
					this.data_type=data.data_type==1?'专车':"拼车"
					this.name=data.name
					this.carno=data.carno
					this.car_attr_id=data.car_attr_id
					this.car_attr_text=data.carattr?data.carattr.name:""
					this.price=data.price
					this.settlement_id=data.settlement_id
					this.settlement_text=data.settlement?data.settlement.name:""
					this.task_category_id=data.task_category_id
					this.task_category_name=data.taskcategory?data.taskcategory.name:""
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			submit(){
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				
				if(this.task_category_name==""){
					httpRequest.toast("请选择任务类型")
					return false
				}
				if(this.car_attr_text=="" && this.task_category_id!=1){
					httpRequest.toast("请选择车辆属性")
					return false
				}
				if(this.start_address=="" && this.task_category_id!=1){
					httpRequest.toast("请选择起始地址")
					return false
				}
				
				if(this.end_address==""){
					httpRequest.toast("请选择目的地址")
					return false
				}
				if(this.data_type==""  && this.task_category_id!=1){
					httpRequest.toast("请选择任务属性")
					return false
				}
				if(this.carno=="" && this.task_category_id!=1){
					httpRequest.toast("请输入航班/车次")
					return false
				}
				if(this.name==""){
					httpRequest.toast("请输入客户姓名")
					return false
				}
				if(this.mobile==""){
					httpRequest.toast("请输入客户电话")
					return false
				}
				if(this.score==""){
					httpRequest.toast("请输入奖励积分")
					return false
				}
				if(this.price==""){
					httpRequest.toast("请输入订单收入")
					return false
				}
				if(this.tasktime==""){
					httpRequest.toast("请选择任务时间")
					return false
				}
				if(this.settlement_text==""){
					httpRequest.toast("请选择结算单位")
					return false
				}
				let url = "/api/admintask/addTask"
				let data = {
					// type:this.type=='接送机任务'?1:2,
					start_address:this.start_address,
					start_longitude:this.start_longitude || 0,
					start_latitude:this.start_latitude || 0,
					end_address:this.end_address,
					end_longitude:this.end_longitude,
					end_latitude:this.end_latitude,
					mobile:this.mobile,
					tasktime:this.tasktime,
					score:this.score,
					data_type:this.data_type=='专车'?1:2,
					name:this.name,
					carno:this.carno,
					car_attr_id:this.car_attr_id,
					task_category_id:this.task_category_id,
					price:this.price,
					settlement_id:this.settlement_id
				}
				if(this.listid){
					this.$set(data,"id",this.listid)
					url = "/api/admintask/editTask"
				}
				httpRequest.request(url, 'GET', data, false, false, true).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg);
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
				
			},
			getjsdw() {
				httpRequest.request('/api/admintask/settlementIndex', 'GET', {}, false, false, true).then(res => {
					this.jsdwcolumns.push(res.data)
					this.show1 = true
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			getsx() {
				httpRequest.request('/api/user/carAttrIndex', 'GET', {}, false, false, true).then(res => {
					this.sxcolumns.push(res.data)
					this.show2 = true
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			getrwlx() {
				httpRequest.request('/api/taskcl/taskCategortyIndex', 'GET', {}, false, false, true).then(res => {
					this.rwlxcolumns.push(res.data)
					this.show3 = true
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			sxconfirm(e) {
				console.log(e,1234);
				this.car_attr_id = e.value[0].id
				this.car_attr_text = e.value[0].name
				this.sxshow = false
			},
			rwlxconfirm(e){
				this.task_category_id = e.value[0].id
				this.task_category_name = e.value[0].name
				this.rwlxshow = false
			},
			jsdwconfirm(e) {
				console.log(e,1234);
				this.settlement_id = e.value[0].id
				this.settlement_text = e.value[0].name
				this.jsdwshow = false
			},
			timeConfirm(e) {
				this.tasktime = e
			},
			changelx() {
				let that = this
				uni.showActionSheet({
					itemList: ['接送机任务', '其他任务'],
					success: function(res) {
						console.log(res);
						if (res.tapIndex == 0) {
							that.type = "接送机任务"
						}
						if (res.tapIndex == 1) {
							that.type = "其他任务"
						}
					},
					fail: function(res) {}
				});
			},
			changesx() {
				let that = this
				uni.showActionSheet({
					itemList: ['专车', '拼车'],
					success: function(res) {
						console.log(res);
						if (res.tapIndex == 0) {
							that.data_type = "专车"
						}
						if (res.tapIndex == 1) {
							that.data_type = "拼车"
						}
					},
					fail: function(res) {}
				});
			},
			startFn() {
				let that = this
				uni.chooseLocation({
					success: function(response) {
						console.log('开始位置名称:', response);
						that.start_address = response.name=="地图位置"?response.address:response.name
						that.start_latitude = response.latitude
						that.start_longitude = response.longitude
					},
					fail(err) {
						console.log('失败:', err);
					}
				})
			},
			endFn() {
				let that = this
				uni.chooseLocation({
					success: function(response) {
						console.log('结束位置名称:', response);
						that.end_address = response.name=="地图位置"?response.address:response.name
						that.end_latitude = response.latitude
						that.end_longitude = response.longitude
					},
					fail(err) {
						console.log('失败:', err);
					}
				})
			},
		}
	}
</script>

<style>

</style>